/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package gestionedt.pools;

import gestionedt.controllers.PoolJpaController;
import gestionedt.models.Pool;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.actions.DispatchAction;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;

/**
 *
 */
public class PoolsAction extends DispatchAction {
    
    /**
     * Liste tous les pools.
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     * @throws java.lang.Exception
     */
    public ActionForward lister(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        PoolsForm pForm = (PoolsForm) form;
        PoolJpaController ct = new PoolJpaController();
        pForm.setPools(ct.getList());

        return mapping.findForward("liste");
    }

    /**
     * Affiche le formulaire d'ajout d'un nv pool.
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     * @throws java.lang.Exception
     */
    public ActionForward afficherNv(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        return mapping.findForward("formPool");
    }

    /**
     * Affiche le formulaire de modification d'un pool existant.
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     * @throws java.lang.Exception
     */
    public ActionForward afficher(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        PoolsForm pForm = (PoolsForm) form;
        int idPool = pForm.getIdPool();
        PoolJpaController ctPool = new PoolJpaController();
        Pool pool = ctPool.findPoolAvecRel(idPool);
        pForm.setPool(pool);

        return mapping.findForward("formPool");
    }

    /**
     * Supprime un pool.
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     * @throws java.lang.Exception
     */
    public ActionForward supprimer(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        PoolsForm pForm = (PoolsForm) form;
        int idPool = pForm.getIdPool();
        PoolJpaController ctPool = new PoolJpaController();
        ctPool.destroy(idPool);

        return mapping.findForward("lister");
    }

}